/*
 * @name:PlaceHolder
 * @author:yanmu.wj@taobao.com
 * @date:2011-11-24
 */
KISSY.add('PlaceHolder',function(S){
    var PlaceHolder = function(cfg){
        this.input = cfg.input && S.one(cfg.input);
        this.placeholder = cfg.placeholder && S.one(cfg.placeholder);
        this.blurCls = cfg.blurCls || 'ph_blur'
    };
    var _PlaceHolder = function(cfg){
        return new PlaceHolder(cfg);
    };
    S.augment(PlaceHolder,{
        init:function(){
            if(!this.input || !this.placeholder) return this;

            var self = this;
            this.fix();
            this.placeholder.on('click',function(){
                self.input.fire('focus');
            });
            this.input.on('keyup',function(){
                if(self.input.val().length){
                    self.placeholder.hide();
                }else{
                    self.placeholder.show();
                    self.placeholder.addClass(self.blurCls);
                }
            }).on('blur',function(){
                if(!self.input.val().length){
                    self.placeholder.show();
                    self.placeholder.removeClass(self.blurCls);
                }
            }).on('focus',function(){
                if(self.input.val().length){
                    self.placeholder.hide();
                }else{
                    self.placeholder.show();
                    self.placeholder.addClass(self.blurCls);
                }
            });
        },
        fix:function(){
            var inputOffset = this.input.offset();
            this.placeholder.show().css({
                position:'absolute',
                left:inputOffset.left+7
            }).css({
                top:inputOffset.top+Math.ceil((this.input[0].clientHeight-this.placeholder[0].clientHeight)/2)
            });
        }
    });

    return _PlaceHolder;
});
